package com.guagua.chat.room;
/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sun.nio.cs.ext.ISCII91;

import com.guagua.base.database.ConnectionManager;
import com.guagua.chat.base.BaseDao;
import com.guagua.vas.constants.ConfigConstants;

/** ******************************************************************
* 类概要:房间操作类，对表t_chatroom_info的操作
 * @author 白云鹏
 * @version 2012年7月31日10:11:39
*******************************************************************/
public class ChatRoomDao extends BaseDao{
	/**
	 * 函数说明：根据房间id查询房间基本信息 <br/>
	 * 
	 * @param room_id
	 *            类型:(long)；<br/> 房间id<br/>；
	 * @return 返回结果说明. （List）房间信息集合
	 */
	public List getRoomInfo(long room_id){
		List room_info = new ArrayList();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			String sql="select r.room_id,r.room_face,r.description,r.create_time,r.last_update,r.enabled,r.sort_type,p.photo_path" +
					" from t_chatroom_info r  with(nolock) left join t_photo p with(nolock) on r.room_face=p.id where r.room_id=?";
			con = ConnectionManager.getDataSourceConnection(JNDI_NAME);
			pstmt = con.prepareStatement(sql);
			pstmt.setLong(1, room_id);
			
			rs = pstmt.executeQuery();
			while(rs.next()){
				Map map = new HashMap();
				map.put("room_id", rs.getLong("room_id"));
				map.put("room_face", rs.getLong("room_face"));
				map.put("description", rs.getString("description"));
				map.put("create_time", rs.getLong("create_time"));
				map.put("last_update", rs.getLong("last_update"));
				map.put("enabled", rs.getInt("enabled"));
				map.put("sort_type", rs.getInt("sort_type"));
				map.put("imgURL", rs.getString("photo_path")==null?"":ConfigConstants.IMG_URL+rs.getString("photo_path"));
				room_info.add(map);
				
			}
		}catch(Exception sqle){
			sqle.printStackTrace();
		}finally{
			ConnectionManager.close(rs, pstmt, con);
		}
		
		
		return room_info;
	}
	
	/**
	 * 函数说明：添加房间基本信息 <br/>
	 * 
	 * @param room_info
	 *            类型:(ChatRoomInfo)；<br/> 房间信息对象<br/>；
	 * @return 返回结果说明. （boolean）是否操作成功，true为成功，false为失败
	 */
	public boolean addRoomInfo(ChatRoomInfo room_info){
		boolean flag=false;
		Connection con = null;
		PreparedStatement pstmt = null;
		try{
			String sql="insert into t_chatroom_info(room_id,room_face,description,create_time,last_update,enabled)values" +
					   "(?,?,?,dbo.fn_datetime_to_timestamp(getdate()),dbo.fn_datetime_to_timestamp(getdate()),1)";
			con = ConnectionManager.getDataSourceConnection(JNDI_NAME);
			pstmt = con.prepareStatement(sql);
			pstmt.setLong(1, room_info.getRoom_id());
			pstmt.setLong(2, room_info.getRoom_face());
			pstmt.setString(3,room_info.getDescription());
			
		    pstmt.execute();
		    flag=true;
		}catch(Exception sqle){
			sqle.printStackTrace();
		}finally{
			ConnectionManager.close(pstmt, con);
		}
		return flag;
		
	}
	/**
	 * 函数说明：修改房间基本信息 <br/>
	 * 
	 * @param room_info
	 *            类型:(ChatRoomInfo)；<br/> 房间信息对象<br/>；
	 * @return 返回结果说明. （boolean）是否操作成功，true为成功，false为失败
	 */
	public boolean udateRoomInfo(ChatRoomInfo room_info){
		boolean flag=false;
		Connection con = null;
		PreparedStatement pstmt = null;
		try{
			String sql="update t_chatroom_info set room_face="+(room_info.getRoom_face()==0?"room_face":room_info.getRoom_face())+",description=?,last_update=dbo.fn_datetime_to_timestamp(getdate()),enabled=?" +
					   " where room_id=?";
			con = ConnectionManager.getDataSourceConnection(JNDI_NAME);
			pstmt = con.prepareStatement(sql);
			//pstmt.setLong(1, room_info.getRoom_face());
			pstmt.setString(1, room_info.getDescription());
			pstmt.setInt(2,1);
			pstmt.setLong(3, room_info.getRoom_id());
			
		    pstmt.execute();
		    flag=true;
		}catch(Exception sqle){
			sqle.printStackTrace();
		}finally{
			ConnectionManager.close(pstmt, con);
		}
		return flag;
	}
	

	/**
	 * 函数说明：修改房间排序类型 <br/>
	 * 
	 * @param room_info
	 *            类型:(ChatRoomInfo)；<br/> 房间信息对象<br/>；
	 * @return 返回结果说明. （boolean）是否操作成功，true为成功，false为失败
	 */
	public boolean udateRoomType(long roomid,int sorttype){
		boolean flag=false;
		Connection con = null;
		PreparedStatement pstmt = null;
		try{
			String sql="update t_chatroom_info set sort_type=? where room_id=?";
			con = ConnectionManager.getDataSourceConnection(JNDI_NAME);
			pstmt = con.prepareStatement(sql);
			//pstmt.setLong(1, room_info.getRoom_face());
			pstmt.setInt(1, sorttype);
			pstmt.setLong(2,roomid);
			
		    pstmt.execute();
		    flag=true;
		}catch(Exception sqle){
			sqle.printStackTrace();
		}finally{
			ConnectionManager.close(pstmt, con);
		}
		return flag;
	}
	
	/**
	 * 添加房间登录信息
	 * @param uid	用户ID
	 * @param roomid
	 * @param ip
	 * @param opTime
	 * @return
	 */
	public int addRoomLoginLog(long uid,long roomid,String ip){
		int result = 0;
		Connection con = null;
		PreparedStatement pstmt = null;
		try{
			String sql="insert into  t_login_log_"+ChatRoomUtil.getCurrYearMonth("_")+"(room_id,user_id,ip,op_time) values(?,?,?,?)";
			con = ConnectionManager.getDataSourceConnection(JNDI_NAME);
			pstmt = con.prepareStatement(sql);
			pstmt.setLong(1, roomid);
			pstmt.setLong(2, uid);
			pstmt.setString(3,ip);
			pstmt.setLong(4, System.currentTimeMillis());			
		    result = pstmt.executeUpdate();
		}catch(Exception sqle){
			sqle.printStackTrace();
		}finally{
			ConnectionManager.close(pstmt, con);
		}
		return result;
	}
	/**
	 * 获取房主有哪些房间
	 * @param userId
	 * @return
	 */
	public List<RoomVo> getUserRoom(long userId){
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<RoomVo> list = new ArrayList<RoomVo>();
		try {
			con = ConnectionManager.getDataSourceConnection(CHATROOM_JNDI_NAME);
			String sql = "SELECT a.room_id, b.name FROM t_room_manager AS a with(NOLOCK), t_chatroom AS b with(NOLOCK) WHERE a.room_id=b.room_id AND a.user_role=100 AND a.user_id="+ userId;
			stmt = con.prepareStatement(sql);
			rs = stmt.executeQuery();
			while(rs.next()){
				RoomVo vo = new RoomVo();
				vo.setRoomId(rs.getLong(1));
				vo.setRoomName(rs.getString(2));
				list.add(vo);
			}
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionManager.close(rs,stmt, con);
		}
		return list;
	}

}
